package com.dong.mqtt;

import com.dong.beans.Zone1AlarmResult;
import com.dong.component.AlarmHandler;
import com.dong.mybatis.entity.Zone1;
import com.dong.mybatis.entity.Zone2;
import com.dong.mybatis.entity.Zone3;
import com.dong.mybatis.mapper.Zone1AlarmRecordMapper;
import com.dong.mybatis.service.impl.Zone1ServiceImpl;
import com.dong.mybatis.service.impl.Zone2ServiceImpl;
import com.dong.mybatis.service.impl.Zone3ServiceImpl;
import com.dong.service.Zone1AlarmService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class EmqMsgProcess implements MqttCallback {
    @Resource
    Zone1AlarmService zone1AlarmService;
    @Resource
    AlarmHandler alarmHandler;
    @Autowired
    private Zone1ServiceImpl zone1Service;

    @Autowired
    private Zone2ServiceImpl zone2Service;

    @Autowired
    private Zone3ServiceImpl zone3Service;

    public Map<String, List<Double>> threshold=new HashMap<>();

    private final ObjectMapper mapper = new ObjectMapper();
    @Autowired
    private Zone1AlarmRecordMapper zone1AlarmRecordMapper;
    @Override
    public void connectionLost(Throwable cause) {
        log.warn("⚠️ MQTT 连接断开: {}", cause.getMessage());
        // 这里不用重连逻辑，连接断开会自动重连，或者交由 EmqClient 处理
    }



    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String payload = new String(message.getPayload());
        log.info("📩 收到消息 - Topic: {}, 内容: {}", topic, payload);
        log.info("打印出数据阈值{}",threshold.toString());
        try {
//            Map<String, Double> paramValues = new HashMap<>();
            Zone1 zone1 = mapper.readValue(payload, Zone1.class);
            switch (topic) {
                case "zone1":

                    log.info("✅ 解析Zone1数据成功: {}", zone1.toString());
                    boolean save1 = zone1Service.save(zone1);
                    log.info(save1 ? "❤️保存Zone1成功" : "❌保存Zone1失败");
                    alarmHandler.handleZone1Data(zone1,"zone1");
                    break;
                case "zone2":
                    Zone2 zone2 = mapper.readValue(payload, Zone2.class);
                    log.info("✅ 解析Zone2数据成功: {}", zone2.toString());
                    boolean save2 = zone2Service.save(zone2);
                    BeanUtils.copyProperties(zone2,zone1);
                    alarmHandler.handleZone1Data(zone1,"zone2");
                    log.info(save2 ? "❤️保存Zone2成功" : "❌保存Zone2失败");
                    break;
                case "zone3":
                    Zone3 zone3 = mapper.readValue(payload, Zone3.class);
                    log.info("✅ 解析Zone3数据成功: {}", zone3.toString());
                    boolean save3 = zone3Service.save(zone3);
                    BeanUtils.copyProperties(zone3,zone1);
                    alarmHandler.handleZone1Data(zone1,"zone3");

                    log.info(save3 ? "❤️保存Zone3成功" : "❌保存Zone3失败");
                    break;
                // 可扩展其他主题处理
                default:
                    log.warn("⚠️ 未知主题: {}", topic);
            }
        } catch (Exception e) {
            log.error("❌ JSON 解析失败 - Topic: {}, 错误: {}", topic, e.getMessage());
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.debug("✅ 消息发送完成");
    }
}
